package com.controller.backend;

import com.alibaba.fastjson.JSON;
import com.biz.HotelAdminBiz;
import com.biz.HotelBiz;
import com.biz.SysOperatingLogBiz;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.Hotel;
import com.entity.model.HotelAdmin;
import com.entity.model.ManagerInfo;
import com.entity.model.SysOperatingLog;
import com.github.pagehelper.PageInfo;
import com.util.SystemConstant;
import com.util.webpage.PageData;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by jc on 2019/11/16.
 */
@Controller
@RequestMapping("/backend/security/hotel")
public class HotelController extends BaseController<Hotel> {
    @Autowired
    private HotelBiz hotelBiz;

    @Autowired
    private HotelAdminBiz hotelAdminBiz;

    @Autowired
    private SysOperatingLogBiz sysOperatingLogBiz;


    //民宿列表
    @RequestMapping(value = "index")
    public String index() throws Exception {
        return "/backend/security/hotel/list";
    }

    //民宿详情
    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/backend/security/hotel/form";
    }

    @RequestMapping(value = "checkInDays")
    public String checkInDays() throws Exception {
        return "/backend/security/hotel/checkInDaysForm";
    }

    //获取门店信息以及上下架状态
    @RequestMapping(value = "findHotelList")
    @ResponseBody
    public Result findHotelList(HttpServletRequest request) {
        PageData cond = new PageData(request);
        List<Hotel> list = hotelBiz.findHotelList(cond);
        PageInfo<Hotel> pageInfo = new PageInfo<Hotel>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }


    //修改民宿基础信息
    @RequestMapping(value = "updateHotelData", method = RequestMethod.POST)
    @ResponseBody
    public Result updateShopData(Hotel hotel, HttpSession session) throws Exception {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
        SysOperatingLog sysOperatingLog = new SysOperatingLog();
        sysOperatingLog.setBeforeLog(JSON.toJSONString(hotel));
        sysOperatingLog.setOperatingId(managerInfo.getId());
        sysOperatingLog.setOperatingName(managerInfo.getUserName());


        if (hotel != null) {
            Condition hotelAdminCond = new Condition(HotelAdmin.class);
            hotelAdminCond.createCriteria().andEqualTo("isSuper", 1).andEqualTo("hotelId", hotel.getId());
            List<HotelAdmin> hotelAdminList = hotelAdminBiz.findByCondition(hotelAdminCond);
            if (hotelAdminList.size() > 0) {
                int len = hotel.getMobile().length();
                HotelAdmin hotelAdmin = hotelAdminList.get(0);
                hotelAdmin.setAccount(hotel.getMobile());
                hotelAdmin.setPassword(DigestUtils.md5Hex(hotelAdmin.getAccount() + hotelAdmin.getAccount().substring(len - 6, len)));
                hotelAdminBiz.update(hotelAdmin);
            }

            Condition hotelCond = new Condition(Hotel.class);
            hotelCond.createCriteria().andEqualTo("id", hotel.getId());
            hotelBiz.updateByCond(hotel, hotelCond);

            sysOperatingLog.setAfterLog(JSON.toJSONString(hotel));
            sysOperatingLog.setRemark("修改民宿基本数据");
            sysOperatingLogBiz.save(sysOperatingLog);

            return ResultGenerator.genSuccessResult(hotel);
        } else {
            return ResultGenerator.genFailResult("操作失败");
        }
    }

    //新增民宿基础信息
    @RequestMapping(value = "addHotelInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result addShopInfo(Hotel hotel, HttpSession session) throws Exception {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
        SysOperatingLog sysOperatingLog = new SysOperatingLog();
        sysOperatingLog.setBeforeLog(null);
        sysOperatingLog.setOperatingId(managerInfo.getId());
        sysOperatingLog.setOperatingName(managerInfo.getUserName());

        //判断店长号码是否存在
        Condition adminCond = new Condition(HotelAdmin.class);
        adminCond.createCriteria().andEqualTo("account", hotel.getMobile());
        if (hotelAdminBiz.findByCondition(adminCond).size() > 0) {
            return ResultGenerator.genFailResult("店长手机号码已存在,请更换手机号");
        }

        hotelBiz.save(hotel);

        sysOperatingLog.setAfterLog(JSON.toJSONString(hotel));
        sysOperatingLog.setRemark("新增民宿");
        sysOperatingLogBiz.save(sysOperatingLog);

        //创建民宿超级管理账号 默认密码手机号后6位
        int len = hotel.getMobile().length();
        HotelAdmin hotelAdmin = new HotelAdmin();
        hotelAdmin.setAccount(hotel.getMobile());
        hotelAdmin.setHotelId(hotel.getId());
        hotelAdmin.setUserName("店长");
        hotelAdmin.setIsSuper(1);
        hotelAdmin.setPassword(DigestUtils.md5Hex(hotelAdmin.getAccount() + hotelAdmin.getAccount().substring(len - 6, len)));
        hotelAdminBiz.save(hotelAdmin);
        return ResultGenerator.genSuccessResult();
    }


    @RequestMapping(value = "findHotelForm")
    @ResponseBody
    public Result findHotelForm(HttpServletRequest request) {
        PageData cond = new PageData(request);
        List<Hotel> list = hotelBiz.findHotelForm(cond);
        PageInfo<Hotel> pageInfo = new PageInfo<Hotel>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }


    @RequestMapping(value = "resetPwd")
    @ResponseBody
    public Result resetPwd(Integer id, String password, String mobile) {
        //int len = mobile.length();
        HotelAdmin hotelAdmin = hotelAdminBiz.findBy("account", mobile);
        hotelAdmin.setPassword(DigestUtils.md5Hex(hotelAdmin.getAccount() + password));
        hotelAdminBiz.update(hotelAdmin);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping(value = "checkInDaysList")
    @ResponseBody
    public Result checkInDaysList(HttpServletRequest request) {
        PageData cond = new PageData(request);
        List<Hotel> list = hotelBiz.checkInDaysList(cond);
        PageInfo<Hotel> pageInfo = new PageInfo<Hotel>(list);
        Integer total = (int) pageInfo.getTotal();
        return ResultGenerator.genSuccessResult(list, total);
    }
}
